package cn.nawang.ebeim.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.nawang.ebeim.client.bean.BackupQueue;
import cn.nawang.ebeim.client.bean.BackupTask;
import cn.nawang.ebeim.client.bean.UploadQueue;

public class BackupThread implements Runnable {
  private static final Logger LOG = LoggerFactory.getLogger(BackupThread.class);

  public void run() {
    LOG.info("backup thread run at {}", System.currentTimeMillis());
    while (!AppMain.stopFlag.get()) {
      /** 获取备份任务 */
      try {
        BackupTask task = BackupQueue.dequeue();
        LOG.debug("get backup task from queue:" + task);
        task.execute();
        enqueueUpload(task);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public void enqueueUpload(BackupTask task) {
    LOG.info("enqueueUpload: {}", task);
    try {
      UploadQueue.enqueue(task);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

}
